package player

import (
	"GameServer/Common/utils"
	"encoding/json"

	"github.com/globalsign/mgo/bson"
	log "github.com/sirupsen/logrus"

	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"
)

func (p *Player) MailListRequest(req *sp.MailListRequest) (res *sp.MailListResponse) {
	list, err := p.User.MailList()
	if err != nil {
		res = &sp.MailListResponse{}
		return
	}

	res = &sp.MailListResponse{
		List: lib.FormatMailsSps(list),
	}
	return
}

func (p *Player) MailReadRequest(req *sp.MailReadRequest) (res *sp.MailReadResponse) {
	p.User.GetUserMail().ReadMails(req.Ids)
	res = &sp.MailReadResponse{}
	return
}

func (p *Player) MailDeleteRequest(req *sp.MailDeleteRequest) (res *sp.MailDeleteResponse) {
	p.User.GetUserMail().DeleteMails(req.Ids)
	res = &sp.MailDeleteResponse{}
	return
}

func (p *Player) MailResRequest(req *sp.MailResRequest) (res *sp.MailResResponse) {
	uProp, resGoods, boxResList, err := lib.RewardsMails("MailResRequest", p.User, req.Ids, p.Send)
	if err != nil {
		res = &sp.MailResResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	res = &sp.MailResResponse{
		Res:    lib.FormatGoodsInfoSps(resGoods),
		BoxRes: lib.FormatBoxResSps(boxResList),
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})

	return
}

func SendMails(typ int64, uids []string, title string, content string, res string) (err error) {
	// res = `[{"tid":220001, "num":100000}, {"tid":220002, "num":22}, {"tid":220004, "num":10}]`
	resGoods := make([]*m.Goods, 0)
	if res != "" {
		err = json.Unmarshal([]byte(res), &resGoods)
		if err != nil {
			return
		}
	}
	mail := &m.Mail{
		Id:      bson.NewObjectId(),
		Title:   title,
		Content: content,
		Res:     resGoods,
		Time:    utils.TNow().Unix(),
		Type:    typ,
	}
	if typ == 1 {
		if err = m.AddSysMails(mail); err != nil {
			log.WithFields(log.Fields{
				"method": "SendMails",
				"data":   mail,
			}).Error("发送全服邮件失败")
			return
		}
		spMail := &sp.MailNoticeRequest{
			MailData: lib.FormatMailSp(mail),
		}
		for _, p := range PlayersModule.GetOnlinePlayers() {
			if p == nil {
				continue
			}
			p.Send(spMail)
			// p.User.GetUserMail().AddSysMail(mail)
		}
		return
	}

	for _, uid := range uids {
		p, ok := GetPlayerByUid(uid)
		if !ok {
			continue
		}
		rmail := p.User.GetUserMail().AddMail(mail)
		p.Send(&sp.MailNoticeRequest{MailData: lib.FormatMailSp(rmail)})
	}
	return
}

func (p *Player) SendMail(title string, content string, res string) (err error) {
	resGoods := make([]*m.Goods, 0)
	if res != "" {
		err = json.Unmarshal([]byte(res), &resGoods)
		if err != nil {
			return err
		}

	}
	mail := &m.Mail{
		Title:   title,
		Content: content,
		Res:     resGoods,
		Time:    utils.TNow().Unix(),
		Type:    0,
	}

	rmail := p.User.GetUserMail().AddMail(mail)
	p.Send(&sp.MailNoticeRequest{MailData: lib.FormatMailSp(rmail)})
	return
}

func (p *Player) SendHeroMail(title string, content string, resGoods []*m.Goods) (err error) {
	mail := &m.Mail{
		Title:   title,
		Content: content,
		Res:     resGoods,
		Time:    utils.TNow().Unix(),
		Type:    2,
	}

	rmail := p.User.GetUserMail().AddMail(mail)
	p.Send(&sp.MailNoticeRequest{MailData: lib.FormatMailSp(rmail)})
	return
}

func SendMailToUid(uid string, title string, content string, resGoods []*m.Goods) {
	if resGoods != nil && len(resGoods) > 0 {

	}
	p, ok := GetPlayerByUid(uid)
	if ok == false {
		log.WithFields(log.Fields{
			"method":    "SendMailToUid",
			"uid":       uid,
			"mailTitle": title,
		}).Error("发送邮件失败，找不到用户")
		return
	}
	mail := &m.Mail{
		Title:   title,
		Content: content,
		Res:     resGoods,
		Time:    utils.TNow().Unix(),
		Type:    0,
	}
	rmail := p.User.GetUserMail().AddMail(mail)
	p.Send(&sp.MailNoticeRequest{MailData: lib.FormatMailSp(rmail)})
}
